/**
 * Created by Administrator on 2017/1/14.
 */
$(function() {
  $("body").on("click", ".btn-upload", function() {
    //上传图片
    var obj = $(this).prev();
    obj
      .change(function() {
        Preview(obj);
      })
      .trigger("click");
  });
});

//上传图片
function Preview(e) {
  var ext = $(e)
    .val()
    .split(".")[1]
    .toUpperCase();
  if (ext != "PNG" && ext != "GIF" && ext != "JPG" && ext != "JPEG") {
    alert("图片限于png，gif，jpeg，jpg格式！");
    return false;
  } else {
    var url = window.URL.createObjectURL($(e)[0].files[0]);
    $(e)
      .next()
      .find("img")
      .attr("src", url);
    $(e)
      .next()
      .find("input")
      .val('<img style="width:80px;max-height:80px" src="' + url + '"');
    $(e)
      .next()
      .find("span")
      .css("color", "#fff")
      .html("更改图片");
  }
}
